package com.example.administrator.thread_test;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class PostActivity extends Activity {
    Handler handler = new Handler();
    Button bt3;
    EditText tv_user;
    EditText tv_pass;

    TextView tv3;
    String loginUrl = "http://39.108.140.116/basic/web/index.php?r=admin/public/soup";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);


       tv3 = (TextView)findViewById(R.id.tv3);
       bt3 = (Button)findViewById(R.id.bt3);
        bt3.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                tv_user = (EditText) findViewById(R.id.tv_user);
                                tv_pass = (EditText) findViewById(R.id.tv_pass);
                                String username=tv_user.getText().toString();

                                String psd=tv_pass.getText().toString();
                                final String r = login(username,psd);
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        tv3.setText(" 结果："+r);
                                    }
                                });
                            }
                        }).start();
                    }
                }
        );
    }
    public String login(String username, String psd) {
        String msg = ""; //服务器返回结果
        try {
//复制前页post流程代码…
            URL url = new URL(loginUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection)
                    url.openConnection();
            httpURLConnection.setConnectTimeout(5000);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.connect();
//post请求传递参数
            String data = "username=" + username+"&psd=" +psd; //参数之间用&连接
//向服务器发送数据(输出流)
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8"));
            writer.write(data);
            writer.close();
//            return msg;



            int code = httpURLConnection.getResponseCode();
            if (code == HttpURLConnection.HTTP_OK) {
//接收服务器返回的信息（输入流）
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream()));
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    msg+=line+"\n";
                }
//关闭缓冲区和连接
                bufferedReader.close();
                httpURLConnection.disconnect();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
        return msg;
    } //end login
}
